package com.feifan.web.controller;

import com.feifan.pojo.ContentDTO;
import com.feifan.pojo.UserDTO;
import com.feifan.service.ResultObject;
import com.feifan.service.api.UserOperatorService;
import com.feifan.service.impl.MailSend;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * @author Donald
 * @create 2019-03-22 0:21
 */
@Controller
public class MyController {

    @Autowired
    private UserOperatorService userOperatorService;

    @RequestMapping("gettt/{id}")
    @ResponseBody
    public UserDTO getUserInfo(@PathVariable("id") String id) throws SQLException
    {
        return userOperatorService.findAllRolesByUserId(2L);
    }

    @RequestMapping("index")
    public String index()
    {
        return "index";
    }

    @RequestMapping("login")
    public String login()
    {
        System.out.println("<>>>>>>>>>>>>>>>>>>>>>>");
        return "index";
    }

    @ResponseBody
    @RequestMapping("register")
   @RequiresPermissions(value = {"system:user:insert"})
    public Object register( @RequestBody(required = false) UserDTO userDTO)
    {
        // 创建用户实体
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(userDTO.getUsername(),userDTO.getPassword());
        subject.login(token);
        System.out.println(userDTO);
        userDTO.setCreateTime(new Date());
        userDTO.setNickname("半肉菠萝蜜");
        userDTO.setGender("男");
        return userDTO;
    }

    @RequestMapping("list")
    public String getListString(@RequestBody(required = false) List<String> lists)
    {
        System.out.println(lists);
        return "index";
    }

    @Autowired
    private MailSend mailSend;

    @ResponseBody
    @RequestMapping("/mail")
    public Object sendMail()
    {
        String user = "1284038873@qq.com";
        String title = "一般的邮件";
        String context = "操";
        mailSend.sendTemplate(user,title,context);
        return ResultObject.build("发送邮件已经OK 了");
    }


    @RequestMapping("content")
    @ResponseBody
    private Object operContent()
    {
        ContentDTO dto = new ContentDTO();
        dto.setTheme("hello word");
        dto.setAuthorId(1515L);
        dto.setCreateTime(new Date());
        dto.setModifyTime(new Date());
        dto.setContent("who are you where are you from ? sometime i just want to do something i like but is ");
        userOperatorService.insertContent(dto);
        System.out.println(dto);
        return ResultObject.build("insertOK");
    }
}
